package com.weacar;

import android.app.Application;
import android.content.Context;

import android.support.annotation.NonNull;

import com.apkfuns.logutils.LogUtils;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.weacar.bean.BaseBean;
import com.weacar.bean.UpdateBean;
import com.weacar.config.Constans;
import com.weacar.utils.DesUtil;

import net.grandcentrix.tray.AppPreferences;

import org.lzh.framework.updatepluginlib.UpdateConfig;
import org.lzh.framework.updatepluginlib.base.UpdateParser;
import org.lzh.framework.updatepluginlib.model.Update;
import org.xutils.x;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        if (Constans.IS_TEST) {
            Constans.URL_HEADER = Constans.TEST_URL_HEADER;
        } else {
            Constans.URL_HEADER = Constans.FORMAL_URL_HEADER;
        }
        // app检测更新的配置
        UpdateConfig.getConfig()
                .setUrl(Constans.URL_HEADER + Constans.get_app_update)// 配置检查更新的API接口
                .setUpdateParser(new UpdateParser() {
                    @Override
                    public Update parse(String response) throws Exception {
                        // TODO 此处的response数据为上方检查更新接口所返回回来的数据。
                        // 需要在此对response数据进行解析，并创建出对应的update实体类数据
                        // 提供给框架内部进行使用
                        LogUtils.d("app_update_response = " + response);
                        Gson gson = new Gson();
                        BaseBean baseBean = gson.fromJson(response, BaseBean.class);
                        if (baseBean != null && baseBean.getCode() == 1) {
                            UpdateBean updateBean = gson.fromJson(DesUtil.decrypt(baseBean.getData()), UpdateBean.class);
                            if (updateBean != null) {
                                Update update = new Update();
                                update.setUpdateContent(updateBean.getMsg().getContent());
                                if (updateBean.getMsg().getUpdate() == 1) {
                                    update.setForced(true);
                                } else {
                                    update.setForced(false);
                                }
                                // 启动图的保存
                                if (updateBean.getMsg().getStart_img() != null) {
                                    // 存储数据
                                    AppPreferences appPreferences = new AppPreferences(getApplicationContext());
                                    appPreferences.put("start_img", updateBean.getMsg().getStart_img());
                                }
                                // 是否忽略此版本更新
                                // update.setIgnore(true);
                                update.setUpdateUrl(updateBean.getMsg().getUrl());
                                update.setMd5(updateBean.getMsg().getFile_md5());
                                update.setVersionCode(updateBean.getMsg().getVersion_code());
                                update.setVersionName(updateBean.getMsg().getVersion());
                                return update;
                            } else {
                                return null;
                            }
                        } else {
                            return null;
                        }
                    }
                });
    }


    static {

        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                ClassicsHeader header = new ClassicsHeader(context);
                header.setFinishDuration(0);
                return header;
            }
        });

        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                ClassicsFooter footer = new ClassicsFooter(context);
                footer.setFinishDuration(0);
                return footer;
            }
        });
    }
}
